/*
 * Copyright (c) 2008, Spagettikod | Stockholm
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the Spagettikod | Stockholm nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.spagettikod.t437;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Configurator
{
	private static final String ACCESS_KEY = "access.key";

	private static final String SECRET_KEY = "secret.key";

	private static final String MAIN_WINDOW_X_POS = "main.window.pos.x";

	private static final String MAIN_WINDOW_Y_POS = "main.window.pos.y";

	private static final String MAIN_WINDOW_HEIGHT = "main.window.height";

	private static final String MAIN_WINDOW_WIDTH = "main.window.width";

	private static File file = new File(System.getProperty("user.home") + File.separator + ".t437.properties");

	private static Properties properties = new Properties();

	private boolean saveSecretKey;

	public Configurator()
	{
		if (file.exists())
		{
			try
			{
				properties.load(new FileInputStream(file));
				// If we find the secret key in the properties file we assume it
				// is to be saved.
				if (getSecretKey() != null || getSecretKey().equals(""))
				{
					setSaveSecretKey(false);
				}
				else
				{
					setSaveSecretKey(true);
				}
			}
			catch (FileNotFoundException e)
			{
				// Should not occur
				e.printStackTrace();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}

	public void save()
	{
		String tempSecret = getSecretKey();

		if (!isSecretKeySaved())
		{
			setSecretKey("");
		}

		try
		{
			properties.store(new FileOutputStream(file), "");
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		setSecretKey(tempSecret);
	}

	public String getAccessKey()
	{
		String key = properties.getProperty(ACCESS_KEY);
		if (key != null)
		{
			key.trim();
		}
		return key;
	}

	public void setAccessKey(String key)
	{
		if (key != null)
		{
			key.trim();
		}
		properties.setProperty(ACCESS_KEY, key);
	}

	public String getSecretKey()
	{
		String key = properties.getProperty(SECRET_KEY);
		if (key != null)
		{
			key.trim();
		}
		return key;
	}

	public void setSecretKey(String key)
	{
		if (key != null)
		{
			key.trim();
		}
		properties.setProperty(SECRET_KEY, key);
	}

	public void setSaveSecretKey(boolean value)
	{
		this.saveSecretKey = value;
	}

	public boolean isSecretKeySaved()
	{
		return this.saveSecretKey;
	}

	public boolean isAccessConfigured()
	{
		return (getAccessKey() != null && getSecretKey() != null && getSecretKey().length() > 0 && getAccessKey()
				.length() > 0);
	}

	public void setMainWindowXPosition(int value)
	{
		properties.setProperty(MAIN_WINDOW_X_POS, Integer.toString(value));
	}

	public void setMainWindowYPosition(int value)
	{
		properties.setProperty(MAIN_WINDOW_Y_POS, Integer.toString(value));
	}

	public int getMainWindowXPosition()
	{
		String pos = properties.getProperty(MAIN_WINDOW_X_POS);
		if (pos == null)
		{
			return 0;
		}
		else
		{
			return Integer.parseInt(pos);
		}
	}

	public int getMainWindowYPosition()
	{
		String pos = properties.getProperty(MAIN_WINDOW_Y_POS);
		if (pos == null)
		{
			return 0;
		}
		else
		{
			return Integer.parseInt(pos);
		}
	}

	public void setMainWindowHeight(int value)
	{
		properties.setProperty(MAIN_WINDOW_HEIGHT, Integer.toString(value));
	}

	public void setMainWindowWidth(int value)
	{
		properties.setProperty(MAIN_WINDOW_WIDTH, Integer.toString(value));
	}

	public int getMainWindowHeight()
	{
		String value = properties.getProperty(MAIN_WINDOW_HEIGHT);
		if (value == null)
		{
			return 0;
		}
		else
		{
			return Integer.parseInt(value);
		}
	}

	public int getMainWindowWidth()
	{
		String value = properties.getProperty(MAIN_WINDOW_WIDTH);
		if (value == null)
		{
			return 0;
		}
		else
		{
			return Integer.parseInt(value);
		}
	}
}
